#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2012 OpenWrt.org
# Copyright (C) 2015 wongsyrone


START=60
USE_PROCD=1

smb_header() {
	local interface
	config_get interface $1 interface "loopback lan"

	# resolve interfaces
	. /lib/functions/network.sh
	local interfaces=$(
		local net
		for net in $interface; do
			local device
			network_get_device device "$net" && {
				local subnet
				network_get_subnet  subnet "$net" && echo -n "$subnet "
				network_get_subnet6 subnet "$net" && echo -n "$subnet "
			}

			echo -n "${device:-$net} "
		done
	)

	local name workgroup description charset domain
	local hostname="$(uci_get system.@system[0].hostname)"

	config_get enabled     $1 enabled     "0"
	config_get name        $1 name        "${hostname:-OpenWrt}"
	config_get workgroup   $1 workgroup   "${hostname:-WORKGROUP}"
	config_get description $1 description "Samba on ${hostname:-OpenWrt}"
	config_get charset     $1 charset     "UTF-8"
	config_get DISABLENB   $1 disablenb   "0"

	mkdir -p /var/etc
	sed -e "s#|NAME|#$name#g" \
	    -e "s#|WORKGROUP|#$workgroup#g" \
	    -e "s#|DESCRIPTION|#$description#g" \
	    -e "s#|INTERFACES|#$interfaces#g" \
	    -e "s#|CHARSET|#$charset#g" \
	    /etc/samba/smb.conf.template > /var/etc/smb.conf


	[ "$DISABLENB" == "1" ] && echo  >>/var/etc/smb.conf -e "\tdisable netbios = yes"

	[ -f /etc/krb5.keytab ] && {
		KLIST=`klist -k /etc/krb5.keytab`
		REALM=${KLIST##*@}
		echo  >>/var/etc/smb.conf -e "\tkerberos method = dedicated keytab"
		echo  >>/var/etc/smb.conf -e "\tdedicated keytab file = /etc/krb5.keytab"
		echo  >>/var/etc/smb.conf -e "\trealm = $REALM"
		sed -i -e "s#workgroup = \(.*\)#workgroup = $REALM#g" /var/etc/smb.conf
		workgroup=$REALM
	}

	[ -S /var/run/ldapi ] && {
		echo  >>/var/etc/smb.conf -e "\tldapsam:trusted = yes"
		echo  >>/var/etc/smb.conf -e "\tldapsam:editposix = yes"
		echo  >>/var/etc/smb.conf -e "\tldap ssl = off"
		echo  >>/var/etc/smb.conf -e "\tdomain logons = yes"
		sed -i -e "s#passdb backend = \(.*\)#passdb backend = ldapsam:ldapi:///#g" \
		       /var/etc/smb.conf
	}

	local homes
	config_get_bool homes $1 homes 0
	[ $homes -gt 0 ] && {
		cat <<EOT >> /var/etc/smb.conf

[homes]
	comment = Home Directories
	browseable = no
	writable = yes
	read only = no
	create mask = 0750
EOT
	}

	[ -L /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
}

smb_add_share() {
	local name
	local path
	local users
	#local public
	local writable
	local printable
	local create_mask
	local guest_ok

	config_get name $1 name
	config_get path $1 path
	config_get users $1 users
	#config_get public $1 public
	config_get writable $1 writable
	config_get printable $1 printable
	config_get create_mask $1 create_mask
	config_get guest_ok $1 guest_ok

	[ -z "$name" -o -z "$path" ] && return

	echo -e "\n[$name]\n\tpath = $path" >> /var/etc/smb.conf
	[ -n "$users" ] && echo -e "\tvalid users = $users" >> /var/etc/smb.conf
	[ -n "$public" ] && echo -e "\tpublic = $public" >> /var/etc/smb.conf
	[ -n "$writable" ] && echo -e "\twritable = $writable" >> /var/etc/smb.conf
	[ -n "$printable" ] && echo -e "\tprintable = $printable" >> /var/etc/smb.conf
	[ -n "$create_mask" ] && echo -e "\tcreate mask = $create_mask" >> /var/etc/smb.conf
	[ -n "$guest_ok" ] && echo -e "\tguest ok  = $guest_ok\n\tpublic  = $guest_ok" >> /var/etc/smb.conf
}


init_config() {
	# Create samba dirs
	[ -d /var/lib/samba ] || mkdir -p /var/lib/samba
	[ -d /var/cache/samba ] || mkdir -p /var/cache/samba
	[ -d /var/run/samba ] || mkdir -p /var/run/samba
	[ -d /var/lock ] || mkdir -p /var/lock
	chmod 0755 /var/lock
	
	config_load samba4
	config_foreach smb_header samba
	config_foreach smb_add_share sambashare
}

reload_service() {
	init_config

	killall -HUP smbd
}

service_triggers() {
	procd_add_reload_trigger samba4
}

start_service() {
	init_config

	if [ "1" = "$enabled" ] || [ "on" = "$enabled" ] || [ "true" = "$enabled" ]; then
		procd_open_instance
		procd_set_param command /usr/sbin/smbd -F
		procd_set_param respawn
		procd_close_instance
		# lower priority using renice in busybox
		renice -n 2 $(pidof smbd)

		[ "$DISABLENB" != "1" ] && {
			procd_open_instance
			procd_set_param command /usr/sbin/nmbd -F
			procd_set_param respawn
			procd_close_instance
			# lower priority using renice in busybox
			renice -n 2 $(pidof nmbd)
		}
	else
		echo "samba4 is disabled"
	fi
}
